package view;

import ActionAdapter;

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.border.Border;

public class Login extends JFrame {
	// Connect status constants
	public final int NULL = 0;
	public final int DISCONNECTED = 1;
	public final int DISCONNECTING = 2;
	public final int BEGIN_CONNECT = 3;
	public final int CONNECTED = 4;

	// Orther Instant
	public final String statusMessages[] = { " Error! Could not connect!",
			" Disconnected", " Disconnecting...", " Connecting...",
			" Connected" };

	// Connect state info
	String hostIP = "localhost";
	final int PORT = 1234;
	int connectionStatus = DISCONNECTED;
	boolean isHost = true;
	public String statusString = statusMessages[connectionStatus];
	//
	JTextField ipField, portField;
	JRadioButton hostOption, guestOption;
	JButton connectButton, disconnectButton;
	JLabel statusField;

	ServerSocket sSocket = null;
	Socket socket = null;
	public BufferedReader in = null;
	public PrintWriter out = null;
	PlayPanel playPanel;

	public JPanel initOptionsPane() {
		JPanel pane = null;
		ActionListener buttonListener = null;
		// Create an options pane
		JPanel optionsPane = new JPanel(new GridLayout(5, 1));

		// IP address input
		pane = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		pane.add(new JLabel("Host IP:"));
		ipField = new JTextField(10);
		ipField.setText(hostIP);
		ipField.setEnabled(false);

		pane.add(ipField);
		optionsPane.add(pane);
		// Port address input
		pane = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		pane.add(new JLabel("Port:"));
		portField = new JTextField(10);
		portField.setEditable(true);
		portField.setText(new Integer(PORT).toString());
		pane.add(portField);
		optionsPane.add(pane);

		// Select Host or Client Box
		buttonListener = new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				isHost = e.getActionCommand().equals("host");
				if (isHost) {
					ipField.setEnabled(false);
					ipField.setText("localhost");
					hostIP = "localhost";
				} else
					ipField.setEnabled(true);
			}
		};
		pane = new JPanel(new GridLayout(1, 2));
		ButtonGroup bg = new ButtonGroup();

		hostOption = new JRadioButton("Host", true);
		hostOption.setMnemonic(KeyEvent.VK_H);
		hostOption.setActionCommand("host");

		guestOption = new JRadioButton("Guest", false);
		guestOption.setMnemonic(KeyEvent.VK_G);
		guestOption.setActionCommand("guest");

		bg.add(hostOption);
		bg.add(guestOption);

		pane.add(hostOption);
		pane.add(guestOption);
		optionsPane.add(pane);

		// Connect/Disconnect Button

		pane = new JPanel(new GridLayout(1, 2));
		connectButton = new JButton("Connect");
		connectButton.setMnemonic(KeyEvent.VK_C);
		connectButton.setActionCommand("connect");
		connectButton.setEnabled(true);

		disconnectButton = new JButton("Disconnect");
		disconnectButton.setMnemonic(KeyEvent.VK_D);
		disconnectButton.setActionCommand("disconnect");
		disconnectButton.setEnabled(false);
		pane.add(connectButton);
		pane.add(disconnectButton);
		optionsPane.add(pane);

		// Status Bar
		pane = new JPanel(new FlowLayout());
		pane.setBackground(Color.WHITE);
		statusField = new JLabel();
		statusField.setText(statusString);
		pane.add(statusField);
		optionsPane.add(pane);

		// Return value
		return optionsPane;
	}

	public void setSocket() throws IOException {
		if (isHost) {
			sSocket = new ServerSocket(PORT);
			socket = sSocket.accept();
			playPanel.firstPlay = playPanel.OPLAYER;
		} else {
			socket = new Socket(hostIP, PORT);
			playPanel.firstPlay = playPanel.XPLAYER;
		}
	}

	public Login(PlayPanel playPanel) {
		this.playPanel = playPanel;
		Container contentPane = getContentPane();
		contentPane.add(initOptionsPane());
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setVisible(true);
		setResizable(false);
		setLocationRelativeTo(null);
		pack();
	}

	public static void main(String[] args) {
		new Login(new PlayPanel());
	}
}